<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>homework</title>
    
</head>

<body>
    <button id="addBtn">添加一条新的访客信息</button>
    <button id="delSelectBtn">删除选中</button>
    <button id="sortBtn">年龄从小到大</button>

    <table border="1" cellspacing="0" cellpadding="10">
        <tr>
            <th>序列号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>操作</th>
            <th>选中</th>
        </tr>

    </table>

    <script>
        var info = [{
            name: "胡杭",
            age: 16
        }, {
            name: "胜明",
            age: 22
        }, {
            name: "军毅",
            age: 21
        }, {
            name: "晓华",
            age: 13
        }, {
            name: "盛聪",
            age: 23
        }, {
            name: "侦剑",
            age: 32
        }, {
            name: "红翔",
            age: 25
        }, {
            name: "超维",
            age: 18
        }, {
            name: "士琪",
            age: 22
        }, {
            name: "艳华",
            age: 20
        }];


        let addBtn = document.getElementById("addBtn");
        let sortBtn = document.querySelector("#sortBtn");
        let delSelectBtn = document.querySelector("#delSelectBtn");

        let tbody = document.querySelector("tbody");
        let index = 0;
        //添加
        addBtn.onclick = function () {

            if (index < info.length) {
                let newTr = document.createElement("tr");

                newTr.innerHTML = `
            <td>${index + 1}</td>
            <td>${info[index].name}</td>
            <td>${info[index].age}</td>
            <td><button>删除</button></td>
            <td><input type="checkbox" name="select" id="select"></td>
           `;
               tbody.appendChild(newTr);

                // 删除
                delBtn = tbody.querySelectorAll("button");
                delBtn.forEach(function (el) {
                    el.onclick = function () {
                        el.parentNode.parentNode.remove();
                    }
                });
                index++;
            };

        }
        // 选中删除

        delSelectBtn.onclick = function () {
            sle=tbody.querySelectorAll("input[type='checkbox']");
             sle=tbody.querySelectorAll("input");
            console.log(sle);


             for(let i=0;i<sle.length+1;i++){
                  sle[i].parentNode.parentNode.remove();
             }
        }

        sortBtn.onclick = function () {
            ageAll=tbody.querySelectorAll("tr:not(first-child)");
            agee=tbody.querySelectorAll("td:nth-child(3)");
            console.log(agee);
            console.log(ageAll);
            ageAll_arr=[];
            for(let i =0;i<ageAll.length;i++){
                ageAll_arr.push(ageAll[i]);
            }
            ageAll_arr.sort(function(a,b){
                // console.log(b);
                /* console.log(a.cells[2]); */
                return a.cells[2].innerText-b.cells[2].innerText;

            })
            for(let i = 0; i < ageAll_arr.length; i++) {
            tbody.appendChild(ageAll_arr[i]);
            }
        };


    </script>
</body>

</html>